package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.SysMenuManagerDAO;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.SysMenu;

@Component("sysMenuManagerDAO")
public class SysMenuManagerDAOImp extends SQLUtil implements SysMenuManagerDAO {

	public List<SysMenu> findSysMenuList(String name, int pageIndex,
			int pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from SysMenu model where 1=1 ");
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		queryString.append(" and model.id != 1");
		queryString.append(" order by model.parent asc,model.menuNo asc,model.id asc");
		return (List<SysMenu>) findObjectAll(queryString.toString(), pageIndex,
				pageSize);
	}

	@Override
	public List<SysMenu> findSysMenuList() throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from SysMenu model where 1=1 ");
		queryString.append(" order by model.parent asc,model.menuNo asc,model.id asc");
		return (List<SysMenu>) findObjectAll(queryString.toString());
	}

	@Override
	public List<SysMenu> findSysMenuList(String roleId) throws Exception {
		final String sql = "select a.sysMenu from SysRoleMenu a"
				+ " where a.sysRole='" + roleId + "'"
				+ " order by a.parent asc,a.menuNo asc,a.id asc";
		return (List<SysMenu>) findObjectAll(sql);
	}

	@Override
	public List<SysMenu> findSysMenuList(String pid, String roleId)
			throws Exception {
		String sql = "";
		if (roleId != null && !"".equals(roleId))
			sql = "SELECT m.* FROM sys_menu m,sys_role_menu rm where m.parentId='"
					+ pid
					+ "' and rm.role_id ="
					+ roleId
					+ " and m.id = rm.menu_id ORDER BY m.parentId,m.menu_no,m.id";
		else
			sql = "SELECT m.* FROM sys_menu m  WHERE m.parentId='" + pid
					+ "' ORDER BY m.parentId,m.menu_no,m.id";
		return (List<SysMenu>) findObejctBySQL(sql, SysMenu.class);
	}

	public List<Object[]> findRoleMenuList(String pid, String roleId)
			throws Exception {
		final String sql = "SELECT m.id,m.name,m.url,rm.id as rmid FROM sys_menu m LEFT JOIN (select * from sys_role_menu where role_id = '"
				+ roleId
				+ "') rm ON m.id = rm.menu_id WHERE m.parentId='"
				+ pid + "' order by m.parentId,m.menu_no,m.id asc";
		return (List<Object[]>) findObejctBySQL(sql, null);
	}

	@Override
	public long findSysMenuListCount(String name) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from SysMenu model where 1=1 ");
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		return (Long) findObjectAll(queryString.toString()).get(0);
	}

	@Override
	public void deleteSysMenu(String id) throws Exception {
		final String sql = "delete from SysMenu t where t.id='" + id + "'";
		final String sqlz = "select t from SysMenu t where t.parent.id='" + id
				+ "'";
		List<SysMenu> list = (List<SysMenu>) findObjectAll(sqlz);
		for (int i = 0; i < list.size(); i++) {
			SysMenu sysMenu = list.get(i);
			deleteSysMenu(sysMenu.getId());
		}
		updateObejctByHql(sql);
	}

	@Override
	public String getMaxMenu(String pmenuId) throws Exception {
		int k = pmenuId.length() + 2;
		String sql = "select max(id) from sys_menu where id like '" + pmenuId
				+ "%' and length(id)=" + k;
		Integer max = (Integer) findObejctByHql(sql) + 1;
		return max.toString();
	}

	@Override
	public List findroleHrHouse(String roleid) throws Exception {
		final String sqlz = "select t.id,t.name,f.id as rhId from hr_houses  t left join (select * from sys_role_house  where role_id='"
				+ roleid + "') f on t.id=f.house_id";
		return findObejctBySQL(sqlz, null);
	}

}
